package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.userinfo.CompleteUserForm;
import com.woniuxy.book.controller.form.userinfo.UserLoginForm;
import com.woniuxy.book.controller.form.userinfo.UserRegisterForm;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.entity.UserInfo;
import com.woniuxy.book.param.userinfo.CompleteUserParam;
import com.woniuxy.book.param.userinfo.UserLoginParam;
import com.woniuxy.book.param.userinfo.UserRegisterParam;
import com.woniuxy.book.service.UserInfoService;
import com.woniuxy.book.service.dto.CollectRankDTO;
import com.woniuxy.book.service.dto.ShoppingTotalDTO;
import com.woniuxy.book.service.dto.UserInfoDTO;
import com.woniuxy.book.util.JWTUtil;
import com.woniuxy.book.util.RedisUtil;
import com.woniuxy.book.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */

@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户信息相关接口")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("发送注册验证码")
    @ApiImplicitParam(name = "userPhone",value = "用户手机号")
    @GetMapping("/sendRegisterCode")
    public Result sendRegisterCode(String userPhone) throws Exception {
        userInfoService.sendRegisterCode(userPhone);
        return Result.success("验证码发送成功",null);
    }

    @ApiOperation("发送登录验证码")
    @ApiImplicitParam(name = "userPhone",value = "用户手机号")
    @GetMapping("/sendLoginCode")
    public Result sendLoginCode(String userPhone) throws Exception {
        userInfoService.sendLoginCode(userPhone);
        return Result.success("验证码发送成功",null);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody UserRegisterForm form) throws Exception {
        userInfoService.register(BeanUtil.toBean(form, UserRegisterParam.class));
        return Result.success("注册成功",null);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginForm form, HttpServletResponse resp){
        System.out.println(form);
        UserLoginParam userLoginParam = BeanUtil.toBean(form,UserLoginParam.class);
        UserInfoDTO userInfoDTO = userInfoService.login(userLoginParam);
        userInfoDTO.setRole("用户");
        HashMap<String, Object> map = new HashMap<>();
        map.put("subject",userInfoDTO.getUserId());
        map.put("role","用户");
        String accessToken = JWTUtil.createToken(map, 30);//短token 有效时间30分钟
        String refreshToken = JWTUtil.createToken(map, 3 * 24 * 60);//长token 有效时间三天
        //通过响应头将jwt响应给客户端
        resp.setHeader("token", accessToken);
        redisUtil.set(accessToken,refreshToken);//存入redis中 短token为键 长token为值
        //暴露token
        resp.setHeader("Access-Control-Request-Headers","token");
        return Result.success("登录成功",userInfoDTO);
    }

    @ApiOperation("收藏图书")
    @GetMapping("/user/collect")
    public Result collect(Integer bookId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.collect(userId,bookId);
        return Result.success("收藏该书成功",null);
    }

    @ApiOperation("取消收藏图书")
    @GetMapping("/user/cancelCollect")
    public Result cancelCollect(Integer bookId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.cancelCollect(userId,bookId);
        return Result.success("取消收藏成功",null);
    }

    @ApiOperation("分页查询收藏列表")
    @GetMapping("/user/selectCollectRank")
    public Result selectCollectRank(Integer pageNum,Integer pageSize,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        PageInfo pageInfo = userInfoService.selectCollectRank(pageNum,pageSize,userId);
        System.out.println(pageInfo);
        return Result.success("查询收藏列表成功",pageInfo);
    }

    @ApiOperation("查询收藏排行榜")
    @GetMapping("/user/selectCollectRankList")
    public Result selectCollectRankList(){
        List<CollectRankDTO> collectRankDTOS =  userInfoService.selectCollectRankList();
        return Result.success("查询排行榜成功",collectRankDTOS);
    }

    @GetMapping("/user/addShopping")
    public Result addShopping(Integer bookId,Integer number,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.addShopping(userId,bookId,number);
        return Result.success("添加购物车成功",null);
    }

    @GetMapping("/user/addNumber")
    public Result addNumber(Integer bookId,Integer number,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.addNumber(userId,bookId,number);
        return Result.success("新增数量成功",null);
    }

    @GetMapping("/user/reduceNumber")
    public Result reduceNumber(Integer bookId,Integer number,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.reduceNumber(userId,bookId,number);
        return Result.success("减少数量成功",null);
    }

    @GetMapping("/user/selectShoppingCar")
    public Result selectShoppingCar(Integer pageNum,Integer pageSize,@RequestHeader("token")String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        PageInfo pageInfo = userInfoService.selectShoppingCar(userId,pageNum,pageSize);
        return Result.success("查询购物车信息成功",pageInfo);
    }

    @GetMapping("/user/changeChecked")
    public Result changeChecked(Integer bookId,Boolean checked,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.changeChecked(bookId,checked,userId);
        return Result.success("修改选中状态成功",null);
    }

    @GetMapping("/user/getTotalMoney")
    public Result getTotalMoney(@RequestHeader("token")String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        ShoppingTotalDTO shoppingTotalDTO = userInfoService.getTotalMoney(userId);
        return Result.success("查询总金额成功",shoppingTotalDTO);
    }

    @GetMapping("/user/delShopping")
    public Result delShopping(Integer bookId,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        userInfoService.delShpping(bookId,userId);
        return Result.success("删除购物车商品成功",null);
    }

    @PostMapping("/user/completeUserInfo")
    public Result completeUserInfo(@RequestBody CompleteUserForm form,@RequestHeader("token")String token){
        String refreshToken = redisUtil.get(token);
        Integer userId = (Integer) JWTUtil.getSubject(refreshToken);
        UserInfo userInfo = userInfoService.completeUserInfo(BeanUtil.toBean(form, CompleteUserParam.class),userId);
        return Result.success("完善个人信息成功",userInfo);
    }

}

